<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="ruleLogin"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              placeholder="请输入账号"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="isLoading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import { getTime } from '@/utils/time'
// 引入用户小仓库
import useUserStore from '@/store/modules/user'
let $router = useRouter()
let $route = useRoute()
let userStore = useUserStore()
// 收集账号密码数据
let loginForm = reactive({ username: 'admin', password: '111111' })
// 登录按钮的加载状态
const isLoading = ref(false)
// 点击登录
const login = async () => {
  // 表单全部校验通过后执行
  await loginForms.value.validate()
  // 按钮加载
  isLoading.value = true
  // 点击登录按钮以后做什么
  // 通知仓库发起请求
  userStore
    .userLogin(loginForm)
    .then(() => {
      isLoading.value = false
      // 退出登录传递过来的路由信息
      let redirect: any = $route.query.redirect
      // 请求成功 路由跳转,如果有携带的路由信息则指定跳转路径,否则跳转首页
      $router.push({ path: redirect || '/' })
      ElNotification({
        type: 'success',
        message: '登录成功',
        title: `Hi,${getTime()}好`,
      })
    })
    .catch((error: Error) => {
      isLoading.value = false
      // 请求失败 弹出登录失败信息
      ElNotification.error(error.message)
    })
}
// 表单元素
const loginForms = ref()
// 表单校验规则
const ruleLogin = {
  username: [
    {
      required: true,
      min: 5,
      max: 10,
      message: '长度应为6~10位',
      trigger: 'change',
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 10,
      message: '长度应为6~10位',
      trigger: 'change',
    },
  ],
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    padding: 40px;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
